<template>
  <view>
    <!-- 收货地址的盒子 -->
    <view class="address-choose-box" v-if="JSON.stringify(address) === '{}'">
      <button type="primary" size="mini" @click="chooseAddress">请选择收货地址+</button>
    </view>
    
    <!-- 地址消息 -->
    <view  class="address-info-box" v-else @click="chooseAddress">
      <view class="row1">
        <view class="name">收货人: <text>{{address.userName}}</text></view>
        <view class="phone">电话: 
          <text class="phone-1">{{address.telNumber}}</text>
          <uni-icons type="arrowright"></uni-icons> 
        </view>
      </view>
      
      <view class="row2">
        <view class="row2-left">收货地址:</view>
        <view class="row2-right">{{addressLink}}</view>
      </view>
    </view>
    
    
    <!-- 底部分割线 -->
    <image src="../../static/split.jpg" class="bottom_split"></image>
  </view>
</template>

<script>
  import { mapState, mapMutations, mapGetters } from 'vuex'
  
  export default {
    name:"my-address",
    computed:{
      ...mapState('m_user',['address']),
      ...mapGetters('m_user',['addressLink'])
    },
    data() {
      return {
      };
    },
    methods:{
      ...mapMutations('m_user',['updateAddress']),
      async chooseAddress(){
        // res 返回的长度为2 一个为null对象  一个为存储对象
        const res= await uni.chooseAddress()
        // 返回对象一 和 对象二
        const [err, succ]= await uni.chooseAddress().catch(err => err)
        if(err === null && succ.errMsg === 'chooseAddress:ok'){
          this.updateAddress(succ)
        }
        // 用户没有授权
        if(err && err.errMsg === 'chooseAddress:fail auth deny' || err.errMsg === 'chooseAddress:fail authorize no response'){
          // 调用 this.reAuth() 方法，向用户重新发起授权申请
          this.reAuth()
        }
      },
      // 重新发起收货地址授权
      async reAuth(){
        // 提示用户对地址进行授权
        const [err2, confirmResult] = await uni.showModal({
          content:'检测到您没打开地址权限,是否去设置打开?',
          confirmText: '确认',
          cancelText: '取消',
        })
        // 如果弹框异常, 则直接退出
        if(err2) return
        
        // 点了取消提示
        if(confirmResult.cancel) return uni.$showMsg('您取消了授权!')
        // 点了确认
        if(confirmResult.confirm) return uni.openSetting({
          success: (settingResult) => {
            // true
            if(settingResult.authSetting['scope.address']) return uni.$showMsg("授权成功,请选择地址!")
            // false
            if(!settingResult.authSetting['scope.address']) return uni.$showMsg("您取消了授权!")
          }
        })
      }
    }
  }
</script>

<style lang="scss">
.address-choose-box{
  height: 90px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.bottom_split{
  width: 100%;
  height: 10px;
}
.address-info-box{
  height: 90px;
  display: flex;
  font-size: 14px;
  flex-direction: column;
  justify-content: center;
  padding: 0px 5px;
}
.row1{
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
.phone{
  display: flex;
  align-items: center;
}
.phone-1{
  margin-right: 5px;
}
.row2{
  display: flex;
  align-items: center;
}
.row2-left{
  white-space: nowrap;
}
</style>
